package top.yrlby.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.yrlby.common.BaseContext;
import top.yrlby.common.R;
import top.yrlby.dto.DishDto;
import top.yrlby.pojo.ShoppingCart;
import top.yrlby.service.ShoppingCartService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author baiyang
 * @since 2022-09-05
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("add")
    public R<ShoppingCart>  add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据：{}",shoppingCart);
        //获取当前登录人的购物车
        //1.设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        Long dishId = shoppingCart.getDishId();
        //创建条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //查询条件 ShoppingCart Where Userid=？
        queryWrapper.eq(ShoppingCart::getUserId , currentId);
        //判断添加到购物车的是菜品还是套餐
        if(dishId!= null){//添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {//添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //查询当前菜品或者套餐是否在购物车中
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        if(one != null){
            //如果在，则追加1
            Integer number = one.getNumber();
            one.setNumber(number+1);
            shoppingCartService.updateById(one);
        }else{
            //如果不在，则添加到购物车
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one=shoppingCart;
        }
        return R.success(one);
    }

    /**
     * 查看购物车信息
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("查看购物车...");
        //创建条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper= new LambdaQueryWrapper<>();
        //查询条件 ShoppingCart where UserId = ？
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //根据新增时间排序
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        //MP自动生成的SQL查询存入集合
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        //返回前台数据
        return R.success(list);
    }

    /**
     * 清空购物车信息
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        //创建条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //查询条件 ShoppingCart where UserId = ？
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        //MP自动生成的SQL删除信息
        shoppingCartService.remove(queryWrapper);
        //返回前台'清除购物车成功！'
        return R.success("清除购物车成功！");
    }
}

